<template>
  <div class="food-record-card">
    <div class="food-header">
      今日饮食
      <button class="food-add-btn" @click="showForm = true">+</button>
    </div>
    <div class="food-list">
      <div v-for="meal in meals" :key="meal.key" class="meal-section">
        <div class="meal-title">{{ meal.label }}</div>
        <div v-if="records[meal.key] && records[meal.key].length">
          <div v-for="item in records[meal.key]" :key="item.id" class="meal-item">
            {{ item.food }} - {{ item.calorie }} kcal <span v-if="item.note">({{ item.note }})</span>
          </div>
          <div class="meal-total">小计：{{ mealTotal(meal.key) }} kcal</div>
        </div>
        <div v-else class="food-item empty">暂无记录</div>
      </div>
    </div>
    <div class="food-intake">
      今日摄入：{{ totalCalorie }} kcal
    </div>
    <el-dialog v-model="showForm" title="添加饮食" width="400px">
      <el-form :model="form" @submit.prevent="submit">
        <el-form-item label="餐次">
          <el-select v-model="form.meal_type">
            <el-option label="早餐" value="breakfast"/>
            <el-option label="午餐" value="lunch"/>
            <el-option label="晚餐" value="dinner"/>
          </el-select>
        </el-form-item>
        <el-form-item label="食物">
          <el-input v-model="form.food"/>
        </el-form-item>
        <el-form-item label="重量(克)">
          <el-input v-model.number="form.weight" type="number" min="1"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.note"/>
        </el-form-item>
        <div v-if="autoCalorie !== null" class="auto-calorie-tip">
          自动计算热量：<span style="color:#722ed1">{{ autoCalorie }} kcal</span>
        </div>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import request from '@/utils/request'
import dayjs from 'dayjs'
import eventBus from '@/utils/eventBus'

const showForm = ref(false)
const form = ref({ meal_type: '', food: '', weight: '', note: '' })
const records = ref({ breakfast: [], lunch: [], dinner: [] })
const meals = [
  { key: 'breakfast', label: '早餐' },
  { key: 'lunch', label: '午餐' },
  { key: 'dinner', label: '晚餐' }
]
const autoCalorie = ref(null)

const fetchRecords = async () => {
  const today = dayjs().format('YYYY-MM-DD')
  const res = await request.get('/sport/diet/', { params: { date: today } })
  records.value = res
}

const submit = async () => {
  const today = dayjs().format('YYYY-MM-DD')
  const res = await request.post('/sport/diet/', { ...form.value, date: today })
  if (res.calorie !== undefined && res.calorie !== null) {
    autoCalorie.value = res.calorie
  } else {
    autoCalorie.value = null
  }
  showForm.value = false
  await fetchRecords()
  // 重置表单
  form.value.meal_type = ''
  form.value.food = ''
  form.value.weight = ''
  form.value.note = ''
  // 通知全局卡路里对比组件刷新
  eventBus.emit('diet-updated')
}

const mealTotal = (meal) => {
  return records.value[meal].reduce((sum, item) => sum + (item.calorie || 0), 0)
}
const totalCalorie = computed(() =>
  meals.reduce((sum, meal) => sum + mealTotal(meal.key), 0)
)

onMounted(fetchRecords)
</script>

<style scoped>
.food-record-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(114,46,209,0.08);
  padding: 20px 24px;
  min-height: 120px;
  display: flex;
  flex-direction: column;
}
.food-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 17px;
  color: #722ed1;
  font-weight: bold;
  margin-bottom: 10px;
}
.food-add-btn {
  background: #f3e6ff;
  color: #722ed1;
  border: none;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
}
.food-list {
  flex: 1;
  min-height: 40px;
  margin-bottom: 8px;
}
.meal-section {
  margin-bottom: 8px;
}
.meal-title {
  font-size: 15px;
  color: #722ed1;
  font-weight: bold;
  margin-bottom: 2px;
}
.meal-item {
  font-size: 14px;
  color: #333;
  margin-bottom: 2px;
}
.meal-total {
  font-size: 13px;
  color: #888;
  margin-bottom: 2px;
}
.food-item.empty {
  color: #aaa;
  font-size: 15px;
  text-align: center;
  padding: 8px 0;
}
.food-intake {
  font-size: 14px;
  color: #888;
}
.auto-calorie-tip {
  font-size: 14px;
  margin-bottom: 8px;
}
</style> 